import { reactive, ref } from 'vue';
import { defineStore } from 'pinia';
import { IsEmpty } from '@/utils/common';

// 使用setup模式定义
export const AppStore = defineStore('AppStore', () => {
  // const curMenu = ref(JSON.parse(localStorage.getItem('curMenu') || '{}'));
  const isCloseDrawer = ref(1); /**是否关闭全局抽屉 */
  const token = ref(localStorage.getItem('token') || '{}');
  const userInfo = ref(JSON.parse(localStorage.getItem('userInfo') || '{}'));
  const remUser = ref(JSON.parse(localStorage.getItem('remUser') || '{}'));
  const sysDict = ref(JSON.parse(localStorage.getItem('sysDict') || '{}'));
  const printer = ref(JSON.parse(localStorage.getItem('printer') || '{}'));
  const layoutSkin = ref(localStorage.getItem('layoutSkin') || 'skin-blue');
  const activeIndex = ref(localStorage.getItem('activeIndex') || '{}'); //顶部菜单栏子级高亮项
  const leftActiveIndex = ref(localStorage.getItem('leftActiveIndex') || '{}'); //左侧菜单栏子级高亮项;
  const leftRouterUrl = ref(localStorage.getItem('leftRouterUrl') || '{}');
  const regRevwerObj = ref(localStorage.getItem('regRevwerObj') || '{}');
  const isReloadLeftTools = ref(0); /**是否重新加载左侧快捷菜单 */
  let mergeTestListFinish = ref(''); //合并标本完成触发修改时间戳
  let curInvStoreObj = reactive({}); /**当前仓库 */
  const wklActionFlag = ref(0);/**是否刷新检验结果 */
  const bloodResultFlag = ref(0);/**是否刷新血培养结果 */

  let maininfoObj = reactive({}); /**当前上机记录 */

  /**审核医师状态对象 */
  const _chkDoctorStr = localStorage.getItem('chkDoctor') || sessionStorage.getItem('locChkDoctor') || '{}';
  let chkDoctorObj = reactive(IsEmpty(_chkDoctorStr) ? { id: '', code: '', name: '', caid: '', expressin: -1 } : JSON.parse(_chkDoctorStr) || { id: '', code: '', name: '', caid: '', expressin: -1 });

  /**检验医师状态对象 */
  const _testDoctorStr = localStorage.getItem('testDoctor') || sessionStorage.getItem('locTestDoctor') || '{}';
  let testDoctorObj = reactive(IsEmpty(_testDoctorStr) ? { id: '', code: '', name: '', caid: '', expressin: -1 } : JSON.parse(_testDoctorStr) || { id: '', code: '', name: '', caid: '', expressin: -1 });

  let locPcName = ref('123');
  const _locWorkGroupStr = sessionStorage.getItem('workGroupInfo') || '{}';
  let locWorkGroup = reactive(IsEmpty(_locWorkGroupStr) ? { id: '', rptGroupName: '' } : JSON.parse(_locWorkGroupStr) || { id: '', rptGroupName: '' });

  const _locWorkGroupBacStr = sessionStorage.getItem('workGroupBacInfo') || '{}';
  let locWorkGroupBac = reactive(IsEmpty(_locWorkGroupBacStr) ? { id: '', rptGroupName: '' } : JSON.parse(_locWorkGroupBacStr) || { id: '', rptGroupName: '' });


  /**关闭全局抽屉 */
  function closeFullDrawer() {
    isCloseDrawer.value = isCloseDrawer.value + 1;
  }

  function setLocPcName(value: any) {
    locPcName.value = value;
  }

  function setMergeTestListFinish(value: any) {
    mergeTestListFinish.value = value;
  }

  function setToken(value: any) {
    token.value = value;
    localStorage.setItem('token', value);
  }
  function setUserInfo(value: any) {
    userInfo.value = value;
    localStorage.setItem('userInfo', JSON.stringify(value));
  }
  // 设置角色模块
  function setRoleModel(roleModels: any) {
    userInfo.value.roleModels = roleModels;
    localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  }
  // 设置模块菜单对象
  function setModuleMenuMap(moduleMenuMap: any) {
    isReloadLeftTools.value = new Date().valueOf();
    userInfo.value.moduleMenuMap = moduleMenuMap;
    localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  }
  // 设置模块功能点对象
  function setModulePointMap(modulePointCodeMap: any) {
    userInfo.value.modulePointMap = modulePointCodeMap;
    localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  }
  // 设置报告单元id信息
  function setRptGroupList(rptGroupList: any) {
    userInfo.value.rptGroupList = rptGroupList;
    localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  }
  // 设置检验仪器id信息
  function setInstrSetupList(instrSetupList: any) {
    userInfo.value.instrSetupList = instrSetupList;
    localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  }
  //设置角色菜单
  function setRoleMenu(roleMenus: any) {
    userInfo.value.roleMenus = roleMenus;
    localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  }
  //设置角色权限
  function setRolePermission(rolePmss: any) {
    userInfo.value.rolePermissions = rolePmss;
    userInfo.value.rolePermissionsStr = rolePmss.map((p: any) => p.code).join(',');
    localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  }
  //设置角色
  function setRole(role: any) {
    userInfo.value.role = role;
    localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  }

  //设置用户当前切换的部门
  function setUserCurDept(dept: any) {
    userInfo.value.curDept = dept;
    localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  }

  //设置用户所有部门
  function setUserDepts(depts: any) {
    userInfo.value.depts = depts;
    localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  }

  //设置外接系统用户信息
  function setSystemUsers(systemUsers: any) {
    userInfo.value.systemUsers = systemUsers;
    localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
  }
  function setRemUser(value: any) {
    remUser.value = value;
    localStorage.setItem('remUser', JSON.stringify(value));
  }
  function setSysDict(value: any) {
    sysDict.value = value;
    localStorage.setItem('sysDict', JSON.stringify(value));
  }
  function addSysDict(values: any) {
    for (const val of values) {
      sysDict.value[val.name] = val.value;
    }
    localStorage.setItem('sysDict', JSON.stringify(sysDict.value));
  }
  function setPrinter(value: any) {
    printer.value = value;
    localStorage.setItem('printer', JSON.stringify(value));
  }
  function removePrinter() {
    localStorage.removeItem('printer');
    printer.value = '';
  }
  function setSignOut() {
    // localStorage.removeItem('curMenu');
    // curMenu.value = '';

    localStorage.removeItem('token');
    token.value = '';

    localStorage.removeItem('userInfo');
    userInfo.value = '';

    localStorage.removeItem('remUser');
    remUser.value = '';

    localStorage.removeItem('sysDict');
    sysDict.value = '';
  }
  function setLayoutColor(value: any) {
    localStorage.setItem('layoutSkin', value);
    layoutSkin.value = value;
  }

  // 顶部菜单二级高亮项
  function setActiveIndex(index: any) {
    localStorage.setItem('activeIndex', index);
    activeIndex.value = index;
  }
  // 左侧菜单栏高亮项
  function setLeftActiveIndex(index: any, path: any) {
    localStorage.setItem('leftActiveIndex', index);
    localStorage.setItem('leftRouterUrl', path);
    leftActiveIndex.value = index;
    leftRouterUrl.value = path;
  }

  /**
   * 设置检验人员
   * @param obj
   */
  function setTestDoctorObj(obj: any, isLoaclSave = false) {
    Object.assign(testDoctorObj, obj);
    if (isLoaclSave) {
      localStorage.setItem('testDoctor', JSON.stringify(obj));
      sessionStorage.setItem('locTestDoctor', JSON.stringify(obj));
    }
  }
  /**
   *设置审核人员
   * @param obj
   */
  function setChkDoctorObj(obj: any, isLoaclSave = false) {
    Object.assign(chkDoctorObj, obj);
    if (isLoaclSave) {
      localStorage.setItem('chkDoctor', JSON.stringify(obj));
      sessionStorage.setItem('locChkDoctor', JSON.stringify(obj));
    }
  }

  function setWorkGroupInfo(obj: any, isLoaclSave = false) {
    Object.assign(locWorkGroup, obj);
    if (isLoaclSave) {
      sessionStorage.setItem('workGroupInfo', JSON.stringify(obj));
    }
  }

  function setWorkGroupBacInfo(obj: any, isLoaclSave = false) {
    Object.assign(locWorkGroupBac, obj);
    if (isLoaclSave) {
      sessionStorage.setItem('workGroupBacInfo', JSON.stringify(obj));
    }
  }

  /**
   *清除缓存默认审核人员
   */
  function clearChkDoctorObj() {
    chkDoctorObj.id = '';
    chkDoctorObj.code = '';
    chkDoctorObj.name = '';
    chkDoctorObj.caid = '';
    chkDoctorObj.expressin = null;
    localStorage.removeItem('chkDoctor');
    sessionStorage.removeItem('locChkDoctor');
  }

  /**切换当前仓库 */
  function setCurInvStoreObj(obj: any) {
    Object.assign(curInvStoreObj, obj);
    localStorage.setItem('curInvStore', JSON.stringify(obj));
  }

  /**是否重新加载左侧快捷按钮 */
  function setReloadTools(num: number) {
    isReloadLeftTools.value = new Date().valueOf();
  }

  function setWKLActionState(state: number, v: any) {    
    wklActionFlag.value = state;
    if (wklActionFlag.value >= 10000) {
      wklActionFlag.value = 0;
    }
    Object.assign(maininfoObj, {});
    Object.assign(maininfoObj, v);    
  }

  function setBloodResultState(state: number, v: any) {    
    bloodResultFlag.value = state;
    if (bloodResultFlag.value >= 10000) {
      bloodResultFlag.value = 0;
    }
    Object.assign(maininfoObj, {});
    Object.assign(maininfoObj, v);    
  }

  return {
    // curMenu,
    isCloseDrawer,
    locPcName,
    mergeTestListFinish,
    token,
    userInfo,
    remUser,
    sysDict,
    printer,
    chkDoctorObj,
    testDoctorObj,
    locWorkGroup,
    locWorkGroupBac,
    curInvStoreObj,
    maininfoObj,
    isReloadLeftTools,
    wklActionFlag,
    bloodResultFlag,
    closeFullDrawer,
    // setCurMenu,
    setLocPcName,
    setMergeTestListFinish,
    setToken,
    setUserInfo,
    setRemUser,
    setSysDict,
    setSignOut,
    setRoleModel,
    setModuleMenuMap,
    setModulePointMap,
    setRptGroupList,
    setInstrSetupList,
    setRoleMenu,
    setRolePermission,
    setRole,
    setSystemUsers,
    setUserCurDept,
    setUserDepts,
    addSysDict,
    setPrinter,
    removePrinter,
    setLayoutColor,
    layoutSkin,
    activeIndex,
    setActiveIndex,
    leftActiveIndex,
    setLeftActiveIndex,
    leftRouterUrl,
    //setTestDoctorObj
    setTestDoctorObj,
    setChkDoctorObj,
    clearChkDoctorObj,
    setWorkGroupInfo,
    setWorkGroupBacInfo,
    setCurInvStoreObj /**设置当前仓库 */,
    setReloadTools,
    setWKLActionState,
    setBloodResultState
  };
});
